import { error } from "@tauri-apps/plugin-log";
import { IComponent } from "./base/IComponent";
import "reflect-metadata";
import { Length } from "./units";

class _Button extends IComponent {
    public static id: number = 0;
    private _element: HTMLButtonElement = document.createElement('button');

    constructor(text: string) {
        super(`btn-${_Button.id++}`);
        this._element.textContent = text;
        this._element.id = this._id;
        this._classes.push("btn");
    }

    width(width: Length): this {
        this._element.style.width = this.handleStyleNumber(width);
        return super.width(width);
    }

    height(height: Length): this {
        this._element.style.height = this.handleStyleNumber(height);
        return super.height(height);
    }

    id(id: string): this {
        this._element.id = id;
        return super.id(id);
    }

    build(parentId?: string): void {
        try {
            super._build(this._element, parentId);
        } catch (e) {
            error(`error ${e}`);
        }
    }
}

export function Button(text: string = '') {
    return new _Button(text);
}
